import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker_fork/flutter_cupertino_date_picker_fork.dart';

import 'custom_list_title.dart';
import 'image/base_image.dart';
import 'package:flustars/flustars.dart' as flustars;

class DateRangePickerField extends StatelessWidget {
  final String title;
  final DateTime startDateTime;
  final DateTime endDateTime;
  final ValueChanged<DateTime> onStartChanged;
  final ValueChanged<DateTime> onEndChanged;

  const DateRangePickerField({
    Key key,
    @required this.title,
    this.onStartChanged,
    this.onEndChanged,
    this.startDateTime,
    this.endDateTime,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomListTitle(
      title: RichText(
        text: TextSpan(text: title, style: TextStyleConstant().normal_1_28, children: [
          TextSpan(
            text: '*',
            style: TextStyleConstant().origin_28,
          ),
        ]),
      ),
      trailing: Row(
        children: <Widget>[
          GestureDetector(
            onTap: () {
              DatePicker.showDatePicker(
                context,
                locale: DateTimePickerLocale.zh_cn,
                initialDateTime: startDateTime,
                onConfirm: (dateTime, _) {
                  if (onStartChanged != null) {
                    onStartChanged(dateTime);
                  }
                },
              );
            },
            behavior: HitTestBehavior.translucent,
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 19.w),
              child: Text(
                startDateTime == null ? '请选择' : flustars.DateUtil.formatDate(startDateTime, format: 'yyyy.MM.dd'),
                style: startDateTime == null ? TextStyleConstant().normal_4_28 : TextStyleConstant().normal_1_28,
              ),
            ),
          ),
          Text(
            '至',
            style: TextStyleConstant().normal_1_28,
          ),
          GestureDetector(
            onTap: () {
              if (startDateTime == null) {
                toast('请先选择起始日期');
                return;
              }
              DatePicker.showDatePicker(
                context,
                locale: DateTimePickerLocale.zh_cn,
                initialDateTime: endDateTime,
                minDateTime: startDateTime,
                onConfirm: (dateTime, _) {
                  if (onEndChanged != null) {
                    onEndChanged(dateTime);
                  }
                },
              );
            },
            behavior: HitTestBehavior.translucent,
            child: Padding(
              padding: EdgeInsets.only(right: 10.w, left: 19.w),
              child: Text(
                endDateTime == null
                    ? startDateTime != null
                        ? '长期有效'
                        : '请选择'
                    : flustars.DateUtil.formatDate(endDateTime, format: 'yyyy.MM.dd'),
                style: endDateTime == null ? TextStyleConstant().normal_4_28 : TextStyleConstant().normal_1_28,
              ),
            ),
          ),
          BaseImage(
            'assets/image/arrow_right_icon.png',
            width: 13.w,
            height: 30.w,
            color: ColorConstant.ffcc,
          ),
        ],
      ),
    );
  }
}
